<template>
  <FormulaEditor ref="FormulaEditor" @formulaEditOK="formulaEditOK"/>
</template>

<script>
import {DefaultFormula} from '@/components/form/DefaultFormDefinition';
import FormulaEditor from '@/components/formula/FormulaEditor.vue';
import {isEmpty, isNotEmpty} from '@/utils/CommonUtils';

export default {
  name: 'LineFormulaModal',
  components: {
    FormulaEditor
  },
  data() {
    return {
      visible: false,
      line: {
        id: null,
        formulaId: null,
        DefaultFormula,
        formula: JSON.parse(JSON.stringify(DefaultFormula))
      }
    };
  },
  methods: {
    setData(formDefinition, line) {
      if (isEmpty(formDefinition)) throw new Error('formDefinition不能为空');
      this.formDefinition = formDefinition;
      this.formula = this.getFormula(line);
      this.line = line;
      this.$refs.FormulaEditor.setData(this.formDefinition, this.formula, 'boolean');
    },
    getFormula(line) {
      if (isEmpty(line)) throw new Error('line是空的');
      if (isNotEmpty(line.formula)) return line.formula;
      return JSON.parse(JSON.stringify(DefaultFormula));
    },
    formulaEditOK() {
      this.line.formula = this.formula;
      this.$emit('formulaEdited')
    },
  }
};
</script>


<style scoped>

</style>